#include <iostream>

using ll = long long;
ll const mo = 1000000007;
long long n, m;

ll quick(ll a, ll b)
{
	if (!b) return 1;
	auto ret = quick(a, b / 2);
	ret = (ret * ret) % mo;
	if (b & 1) ret = (ret * a) % mo;
	return ret;
}

void extended_gcd(ll a, ll b, ll & d, ll & x, ll & y)
{
	if (!b) { d = a; x = 1; y = 0; }
	else {
		extended_gcd(b, a % b, d, y, x);
		y -= x * (a/b);
	}
}

ll inverse(ll t, ll p = mo)
{
	ll x, y, d;
	extended_gcd(t, p, d, x, y);
	return (x % p + p) % p;
}

ll calc()
{
	ll ret = quick(m, n + 1) - 1;
	ret = (ret * inverse(m - 1)) % mo;
	return (ret + mo) % mo;
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	int T; std::cin >> T;
	while (T--) {
		std::cin >> n >> m;
		std::cout << calc() << "\n";
	}
}

